import Vuex from 'vuex'
import Vue from 'vue'
 
Vue.use(Vuex)

let doc = JSON.parse(localStorage.doccar||"[]");



export default new Vuex.Store({
  state: {
    prescriptionPro:[],
    userX:{},
    docX:doc,
    upload:false,
  },
  mutations: {
    //常用药单
    prescription:function(state,comm){
      state.prescriptionPro = comm;
    },
    //药单删除
    prescriptionDel:function(state,comm){
      state.prescriptionPro = comm;
    },
    //添加药单（收藏处）
    colletAdd:function(state,comm){
      let zero = comm;
      for(let i=0;i<zero.length;i++){
        for(let j=0;j<state.prescriptionPro.length;j++){
          if(zero[i].id == state.prescriptionPro[j].id){
            state.prescriptionPro[j].num = state.prescriptionPro[j].num + zero[i].num;
            zero.splice(i,1); 
            return
          }
        }
      }
      if(zero.length>0){
        state.prescriptionPro = state.prescriptionPro.concat(zero)
      }
    },
    //清空
    prescriptionEmpty:function(state,comm){
      state.prescriptionPro = [];
    },
    //记录用户的id和名字
    saveUser:function(state,comm){
      state.userX.name = comm.name;
      state.userX.id = comm.id;
    },
    //清空用户的id和名字
    emptyUser:function(state,comm){
      state.userX = {}
    },
    //记录医生的id
    saveDoc:function(state,comm){
      state.docX.id = comm.id
    },
    //清除医生id
    emptyDoc:function(state,comm){
      state.docX = {}
    },
    //上传图片
    upload:function(state,comm){
      state.upload = true;
    },
  },
  getters: {
    getPrescription:function(state){
      return state.prescriptionPro;
    },
    getUser:function(state){
      return state.userX;
    },
    getDoc:function(state){
      return state.docX;
    },
    getupload:function(state){
      return state.upload;
    },
  }
})